//
//  BGMineViewContrller.swift
//  Baga
//
//  Created by 飞王 on 16/12/21.
//  Copyright © 2016年 飞王. All rights reserved.
//

import UIKit



class BGMineViewContrller: BGBaseViewController {
    
    var mineListArr:NSArray?;

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "个人中心";
        loadData();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func loadData() {
        if let bundlePath = Bundle.main.path(forResource: "mineData", ofType: "plist") {
            let listArr = NSMutableArray(contentsOfFile: bundlePath);
            
            
            
            let tempMutableArr = NSMutableArray();
            
            
            for i in 0..<(listArr?.count)! {
                let dict = listArr?[i];
                let cellModel = BGMineCellModel(dict: dict as! [String : AnyObject]);
                tempMutableArr.add(cellModel);
            }
            
            mineListArr = NSArray(array: tempMutableArr);
            
            
            setupViews();
        }
        
    }
    
    private func setupViews() {
        view.addSubview(mianTableView);
    }
    
    
    
//    MARK: - LazyLoad
    private lazy var mianTableView: UITableView = {
        
        let mainTableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH - 44), style: UITableViewStyle.grouped);
        mainTableView.delegate = self;
        mainTableView.dataSource = self;
        
        mainTableView.frame = CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH - 44);
        
        mainTableView.register(UINib(nibName: "BGMineTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "mineTableViewCell");
        
        mainTableView.register(BGMineTableViewHeader.self, forHeaderFooterViewReuseIdentifier: "mineTableViewHeader")
        
        mainTableView.separatorStyle = .none;
        
        return mainTableView;
        
        
    }();

}

extension BGMineViewContrller:BGMineHeaderDelegate {
    func mineHaderLogin() {
        
        let loginController = BGLoginViewController();
        
        let loginNavController = BGPresentNavigationController(rootViewController: loginController);
        
        present(loginNavController, animated: true) { 
            
        };
    }
    
}

extension BGMineViewContrller:UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (mineListArr?.count)!;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "mineTableViewCell");
        if !(cell != nil) {
            cell = BGMineTableViewCell(style: .default, reuseIdentifier: "mineTableViewCell");
        }
        
        cell?.selectionStyle = .none;
        
        
        let index = indexPath.row;
        
        let cellModel:BGMineCellModel = mineListArr![index] as! BGMineCellModel ;
        
        (cell as! BGMineTableViewCell).cellModel = cellModel;
        
        return cell!;

    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        let header : BGMineTableViewHeader = tableView.dequeueReusableHeaderFooterView(withIdentifier: "mineTableViewHeader") as! BGMineTableViewHeader;
        header.delegate = self;

        return header;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 58;
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 140;
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        
    }
}
